package Panels;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import Code.Homework4;
import DatabaseCommands.DatabaseInfo;

public class DatabaseHome extends JPanel implements MouseListener{
	
	
	private static DatabaseHome Instance=null;
	DatabaseInfo DInfo=DatabaseInfo.getInstance();
	private List<JLabel> Labels=new ArrayList<JLabel>();
	private JTree TablesTree;
	protected JTabbedPane TabbedFrame;
	protected JSplitPane SplitPane;

	protected DatabaseHome(){CreateDatabaseHome();}
	
	public void CreateDatabaseHome(){
		
		setLayout(new BorderLayout());
		
		Labels.add(new JLabel("Connected as "+DInfo.DatabaseUserName+" to "+DInfo.DatabaseName));
		Labels.get(0).setFont(new Font("Serif",Font.BOLD,22));
		add(Labels.get(0),BorderLayout.PAGE_START);
		
		
		TabbedFrame=new JTabbedPane();
		TabbedFrame.setFont(new Font("Serif",Font.PLAIN,16));
		
		
		SplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,TableTreeView(),TabbedFrame);
		SplitPane.setOneTouchExpandable(false);
		SplitPane.setDividerLocation(175);
		
		add(SplitPane,BorderLayout.CENTER);
		
	}
	
	
	public void UpdateTableTreeView(){
		int Location=SplitPane.getDividerLocation();
		SplitPane.setLeftComponent(TableTreeView());
		SplitPane.setDividerLocation(Location);
	}
	
	
	public JScrollPane TableTreeView(){
		DefaultMutableTreeNode TopNode=new DefaultMutableTreeNode("Database");
		DefaultMutableTreeNode TablesNode=new DefaultMutableTreeNode("Tables");
		DefaultMutableTreeNode QueryNode=new DefaultMutableTreeNode("Queries");
		DefaultMutableTreeNode Homework4Node=new DefaultMutableTreeNode("Homework4");
		TablesTree=new JTree(TopNode);
		TablesTree.setToggleClickCount(1);
		TopNode.add(TablesNode);
		TopNode.add(QueryNode);
		TopNode.add(Homework4Node);
		
		QueryNode.add(new DefaultMutableTreeNode("Create Tables"));
		QueryNode.add(new DefaultMutableTreeNode("Drop All Tables"));
		QueryNode.add(new DefaultMutableTreeNode("Everything"));
		
		Homework4Node.add(new DefaultMutableTreeNode("Return Author Books"));
		Homework4Node.add(new DefaultMutableTreeNode("Change Book Title"));
		Homework4Node.add(new DefaultMutableTreeNode("Borrowed Books"));
		Homework4Node.add(new DefaultMutableTreeNode("Books Checked Out"));
		
	    JScrollPane TableTreePane=new JScrollPane(TablesTree);
	    TableTreePane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
	    TableTreePane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		int x=0;
		for(String Item:DInfo.GetTables()){
			TablesNode.add(new DefaultMutableTreeNode(Item));
			x++;
		}
		
		TablesTree.expandRow(0);
		TablesTree.expandRow(3);
		TablesTree.expandRow(2);
		TablesTree.expandRow(1);
		
		TablesTree.addMouseListener(this);
		
		return TableTreePane;
	}
	
	
	public void CreateTableView(String TableName,boolean Query){
		for(int x=0;x<=TabbedFrame.getTabCount()-1;x++){
			if(TabbedFrame.getTitleAt(x)==TableName){
				TabbedFrame.setSelectedIndex(x);
				DInfo.SetTable(TableName);
				return;
			}
		}
		if(!Query){
			DInfo.SetTable(TableName);
		}
		TabbedFrame.addTab(TableName,new Table(TableName,Query));
		TabbedFrame.setTabComponentAt(TabbedFrame.getTabCount()-1,new Tab(TableName));
		TabbedFrame.setSelectedIndex(TabbedFrame.getTabCount()-1);
	}
	
	
	public void RemoveTab(String TabName){
		for(int x=0;x<=TabbedFrame.getTabCount()-1;x++){
			if(TabbedFrame.getTitleAt(x)==TabName){
				TabbedFrame.remove(TabbedFrame.getComponentAt(x));
			}
		}
	}
	
	
	public static DatabaseHome getInstance(){
		if(Instance==null){
			Instance=new DatabaseHome();
		}
		return Instance;
	}

	
	
	@Override
	public void mouseClicked(MouseEvent Event){
		TreePath Path=TablesTree.getPathForLocation(Event.getX(),Event.getY());
		if(Path!=null && Event.getClickCount()==2){
			String Node=Path.getLastPathComponent().toString();
			if(Node=="Database" && Path.getParentPath()==null){return;}
			String Parent=Path.getParentPath().getLastPathComponent().toString();
			
			if(Parent=="Tables"){
				CreateTableView(Node,false);
			}else if(Parent=="Queries"){
				CreateTableView(Node,true);
			}else if(Parent=="Homework4"){
				new Homework4(Node);
			}
		}
	}


	@Override
	public void mousePressed(MouseEvent Event){}	
	@Override
	public void mouseReleased(MouseEvent e){}
	@Override
	public void mouseEntered(MouseEvent e){}
	@Override
	public void mouseExited(MouseEvent e){}
	
	
	

}
